<template>
	<view class="home-container" :style="{paddingBottom:paddingBottom+'px'}">
		<!-- 头部区域 -->
		<headerSlot :propsColor='propsColor'>
			<template v-slot:header>
				<view class="header-content">
					<view class="header-left">
						<view class="logo-container">
							<image :src="`${config.commonUrl}/logo-orange.png`" mode="aspectFit"></image>
						</view>
					</view>
					<view class="header-center">
						<text class="welcome-text">欢迎来到惠邻生活</text>
					</view>
					<view class="header-right">
						<view class="community-info">
							<text
								class="community-name">{{(userInfo.community && userInfo.community.name) || '未选择小区'}}</text>
						</view>
					</view>
				</view>
			</template>
		</headerSlot>

		<!-- 公告栏 -->
		<view class="notice-section">
			<uni-notice-bar show-icon scrollable background-color="#FFF7E6" color="#FF6B35" :text="noticeBarText" />
		</view>

		<!-- 主要内容区域 -->
		<view class="main-content">
			<!-- 轮播图区域 -->
			<view class="banner-section" v-if="bannerList.length > 0">
				<swiper class="banner-swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="500"
					indicator-color="rgba(255, 255, 255, 0.5)" indicator-active-color="#FF6B35">
					<swiper-item v-for="(item, index) in bannerList" :key="index" @click="handleBannerClick(item)">
						<image :src="item.image" mode="aspectFill" class="banner-image"></image>
					</swiper-item>
				</swiper>
			</view>

			<!-- 社区生活版块 -->
			<view class="community-section">
				<view class="section-header">
					<view class="section-title">
						<text class="title-text">社区生活</text>
						<view class="title-decoration"></view>
					</view>
					<view class="section-subtitle">
						<text
							class="subtitle-text">{{(userInfo.community && userInfo.community.name) || userInfo.communityName || '未选择小区'}}</text>
					</view>
				</view>

				<!-- 主要服务卡片 -->
				<view class="main-service-card" @click="goPage('/pages/homePages/orderCenter')">
					<view class="service-content">
						<view class="service-info">
							<view class="service-title">
								<text class="title-main">邻居有困难</text>
								<text class="title-icon">💪</text>
							</view>
							<view class="service-desc">
								<text class="desc-text">跑腿、代劳、代缴费、宠物照看...</text>
								<text class="desc-text">大事小事,都有我~</text>
							</view>
						</view>
						<view class="service-image">
							<image src="@/static/xq.png" mode="aspectFit"></image>
						</view>
					</view>
				</view>

				<!-- 快捷服务网格 -->
				<view class="quick-services">
					<view class="service-item primary" @click="goPage('/pages/homePages/sam/samPurchase')">
						<view class="item-content">
							<view class="item-header">
								<text class="item-title">邻里集市</text>
								<image src="@/static/js.png" mode="aspectFit" class="item-icon"></image>
							</view>
							<text class="item-desc">便捷购物</text>
						</view>
					</view>

					<view class="service-item secondary" @click="goPage('/pages/homePages/ranking')">
						<view class="item-content">
							<view class="item-header">
								<text class="item-title">互助排行</text>
								<image src="@/static/ranking2.png" mode="aspectFit" class="item-icon"></image>
							</view>
							<text class="item-desc">前三神秘大奖</text>
						</view>
					</view>

					<view class="service-item tertiary" @click="goPage('/pages/mePages/complaintsSuggestions')">
						<view class="item-content">
							<view class="item-header">
								<text class="item-title">服务入驻</text>
								<image src="@/static/hz.png" mode="aspectFit" class="item-icon"></image>
							</view>
							<text class="item-desc">个人/商户/合作</text>
						</view>
					</view>
				</view>

				<!-- 底部服务图标 -->
				<view class="bottom-services">
					<view class="service-icon-item" @click="goPage('/pages/circlePages/editCircle?type=C')">
						<view class="icon-wrapper">
							<image :src="`${config.commonUrl}/icon2.png`" mode="aspectFit"></image>
						</view>
						<text class="icon-text">闲置转让</text>
					</view>

					<view class="service-icon-item" @click="goPage('/pages/homePages/cleaningService')">
						<view class="icon-wrapper">
							<image :src="`${config.commonUrl}/icon3.png`" mode="aspectFit"></image>
						</view>
						<text class="icon-text">家庭保洁</text>
					</view>

					<view class="service-icon-item" @click="goPage('/pages/homePages/repairService')">
						<view class="icon-wrapper">
							<image :src="`${config.commonUrl}/icon4.png`" mode="aspectFit"></image>
						</view>
						<text class="icon-text">家庭维修</text>
					</view>

					<view class="service-icon-item" @click="goPage('/pages/homePages/editForm')">
						<view class="icon-wrapper">
							<image :src="`${config.commonUrl}/icon1.png`" mode="aspectFit"></image>
						</view>
						<text class="icon-text">下单求助</text>
					</view>
				</view>
			</view>

			<!-- 最新订单区域 -->
			<view class="orders-section" v-if="showOrdersSection && recentOrders.length > 0">
				<view class="orders-header">
					<view class="section-title">
						<text class="title-text">最新订单</text>
						<view class="title-decoration"></view>
					</view>
					<view class="more-link" @click="goPage('/pages/homePages/orderCenter')">
						<text class="more-text">查看更多</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>

				<view class="orders-content">
					<OrderCard :serviceOrderList="recentOrders" :loading="ordersLoading" :noMoreData="true" />
				</view>
			</view>
		</view>

		<TabBar :PageCur="'home'" @height-change="handleBarHeightChange" />
	</view>
</template>

<script>
	import headerSlot from '@/components/header-slot.vue';
	import OrderCard from '@/components/orderCenterComponents/order-card.vue';
	import TabBar from '@/components/tabBar.vue';
	import config from '@/config/config';
	import storage from "@/utils/storage.js";
	import {
		getNotice,
		getBanner
	} from "@/api/home.js";
	import {
		getServiceOrderList
	} from "@/api/order.js";
	import {
		unreadCount,
		getPlatformInfo
	} from "@/api/members.js"
	import subscribe from "@/minixs/subscribe.js"

	export default {
		mixins: [subscribe],
		components: {
			headerSlot,
			OrderCard,
			TabBar,
		},
		data() {
			return {
				paddingBottom: 50,
				TEMPLATE_IDS: [
					'm29dlgGaPLcaNJAt3EswljAewCvh33QkAfzKBYOuFkI',
					'xV6gegv5o_6AHjaBEYxyWpwtZ1c-dFBDaHFiRbG6uhU',
					'0kIkcR2nNQQigc7x_etb9yDzHlAitfRiCFSAC0igmsk'
				],
				config,
				//顶部标签卡背景色
				propsColor: '',
				noticeBarText: '',
				userInfo: {},
				bannerList: [], // 轮播图列表
				showOrdersSection: true, // 控制订单区域显示
				recentOrders: [], // 最新订单列表
				ordersLoading: false, // 订单加载状态
			};
		},


		onLoad() {
			this.userInfo = storage.getUserInfo()
			this.loadBanner() // 加载轮播图
			this.loadRecentOrders() // 加载最新订单
			getNotice().then(res => {
				if (res.data.data && res.data.data.length > 0) {
					res.data.data.forEach((msg, idx) => {
						this.noticeBarText = this.noticeBarText + `${idx+1}.${msg.title}：${msg.content}`
					})
				}
				if (!this.noticeBarText) {
					this.noticeBarText = '惠邻生活上线啦,欢迎各位业主~'
				}
			})
		},

		onShow() {
			// 每次显示页面时刷新最新订单
			this.loadRecentOrders();
		},

		methods: {

			// 加载轮播图
			loadBanner() {
				getBanner().then(res => {
					if (res.data.code === 200 && res.data.data) {
						this.bannerList = res.data.data
					}
				}).catch(err => {
					console.error('获取轮播图失败:', err)
				})
			},

			// 加载最新订单
			loadRecentOrders() {
				if (this.ordersLoading) return;
				
				this.ordersLoading = true;
				getServiceOrderList({
					page: 1,
					limit: 3, // 只获取最新5条
					t: Date.now(),
				}).then(res => {
					const data = res.data && res.data.data;
					const list = data && data.list ? data.list : [];
					this.recentOrders = list;
				}).catch(err => {
					console.error('加载最新订单失败:', err);
					this.recentOrders = [];
				}).finally(() => {
					this.ordersLoading = false;
				});
			},

			// 处理轮播图点击
			handleBannerClick(item) {
				if (item && item.link) {
					// 如果有链接，跳转到对应页面
					if (item.link.startsWith('/pages/')) {
						// 内部页面
						this.$router.smartNavigate({
							url: item.link
						})
					} else if (item.link.startsWith('http')) {
						// 外部链接，暂时不处理
						uni.showToast({
							title: '外部链接暂不支持',
							icon: 'none'
						})
					}
				} else if (item && item.productId) {
					// 如果是商品，跳转到商品详情
					this.$router.smartNavigate({
						url: `/pages/product/detail?id=${item.productId}`
					})
				}
			},


			goPage(route) {
				if (route == '/pages/circlePages/editCircle?type=C&idx=7') {
					this.$store.commit('setEditCircle', {})
				}
				this.$router.smartNavigate(route)
			},

			isAreaId() {
				// 判断是否选择小区
				return storage.getAreaId() ? true : false
			},

			isLogin() {
				// 检查登录状态
				return storage.getAccessToken() ? true : false
			},

			handleBarHeightChange(height) {
				// 动态设置内容区域底部内边距
				this.paddingBottom = height + 10
			},



		}
	}
</script>

<style lang="scss" scoped>
	.home-container {
		background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
		min-height: 100vh;
	}

	// 头部样式
	.header-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 32rpx;
		width: 100%;

		.header-left {
			.logo-container {
				width: 176rpx;
				height: 48rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.header-center {
			flex: 1;
			text-align: center;

			.welcome-text {
				font-size: 28rpx;
				color: #666;
				font-weight: 500;
			}
		}

		.header-right {
			.community-info {
				.community-name {
					font-size: 24rpx;
					color: #999;
					background: rgba(255, 255, 255, 0.8);
					padding: 8rpx 16rpx;
					border-radius: 20rpx;
				}
			}
		}
	}

	// 公告栏样式
	.notice-section {
		margin: 0 24rpx 24rpx;

		::v-deep .uni-noticebar {
			border-radius: 16rpx;
			box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
		}
	}

	// 主要内容区域
	.main-content {
		padding: 0 24rpx;
		background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
		min-height: calc(100vh - 200rpx);
	}

	// 轮播图样式
	.banner-section {
		margin-bottom: 32rpx;
		border-radius: 24rpx;
		overflow: hidden;
		box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.1);
		background: #fff;

		.banner-swiper {
			height: 320rpx;
			border-radius: 24rpx;

			.banner-image {
				width: 100%;
				height: 100%;
				border-radius: 24rpx;
				transition: transform 0.3s ease;

				&:hover {
					transform: scale(1.02);
				}
			}
		}
	}

	// 社区生活版块
	.community-section {
		background: white;
		border-radius: 24rpx;
		padding: 32rpx;
		margin-bottom: 32rpx;
		box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);

		.section-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 32rpx;

			.section-title {
				display: flex;
				align-items: center;

				.title-text {
					font-size: 36rpx;
					font-weight: 700;
					color: #333;
					margin-right: 16rpx;
				}

				.title-decoration {
					width: 8rpx;
					height: 32rpx;
					background: linear-gradient(135deg, #FF6B35 0%, #FE6100 100%);
					border-radius: 4rpx;
				}
			}

			.section-subtitle {
				.subtitle-text {
					font-size: 26rpx;
					color: #666;
					background: #f5f5f5;
					padding: 8rpx 16rpx;
					border-radius: 20rpx;
				}
			}
		}

		// 主要服务卡片
		.main-service-card {
			background: linear-gradient(135deg, #E8F5E8 0%, #D4F1D4 100%);
			border-radius: 20rpx;
			padding: 32rpx;
			margin-bottom: 24rpx;
			position: relative;
			overflow: hidden;
			transition: all 0.3s ease;

			&:active {
				transform: translateY(-2rpx);
				box-shadow: 0 12rpx 40rpx rgba(11, 207, 86, 0.2);
			}

			.service-content {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.service-info {
					flex: 1;

					.service-title {
						display: flex;
						align-items: center;
						margin-bottom: 16rpx;

						.title-main {
							font-size: 48rpx;
							font-weight: 700;
							color: #0bcf56;
							margin-right: 12rpx;
						}

						.title-icon {
							font-size: 32rpx;
						}
					}

					.service-desc {
						.desc-text {
							display: block;
							font-size: 28rpx;
							color: rgba(0, 0, 0, 0.6);
							line-height: 1.5;
							margin-bottom: 8rpx;
						}
					}
				}

				.service-image {
					width: 120rpx;
					height: 120rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		// 快捷服务网格
		.quick-services {
			display: flex;
			flex-wrap: wrap;
			margin: 0 -8rpx 32rpx;

			.service-item {
				flex: 1;
				min-width: 200rpx;
				margin: 8rpx;
				border-radius: 16rpx;
				padding: 24rpx;
				transition: all 0.3s ease;
				position: relative;
				overflow: hidden;

				&:active {
					transform: translateY(-2rpx);
				}

				&.primary {
					background: linear-gradient(135deg, #E6F7FE 0%, #D1F2FE 100%);
				}

				&.secondary {
					background: linear-gradient(135deg, #EAFEFC 0%, #D4FCF8 100%);
				}

				&.tertiary {
					background: linear-gradient(135deg, #FAFFE9 0%, #F0FFD1 100%);
				}

				.item-content {
					.item-header {
						display: flex;
						justify-content: space-between;
						align-items: center;
						margin-bottom: 12rpx;

						.item-title {
							font-size: 28rpx;
							font-weight: 600;
							color: #333;
						}

						.item-icon {
							width: 40rpx;
							height: 40rpx;
						}
					}

					.item-desc {
						font-size: 22rpx;
						color: rgba(0, 0, 0, 0.6);
					}
				}
			}
		}

		// 底部服务图标
		.bottom-services {
			display: flex;
			justify-content: space-between;
			align-items: flex-start;

			.service-icon-item {
				flex: 0 0 22%;
				display: flex;
				flex-direction: column;
				align-items: center;
				transition: all 0.3s ease;

				&:active {
					transform: scale(0.95);
				}

				.icon-wrapper {
					width: 88rpx;
					height: 88rpx;
					background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-bottom: 16rpx;
					box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.1);
					transition: all 0.3s ease;

					&:active {
						transform: scale(0.9);
						box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
					}

					image {
						width: 52rpx;
						height: 52rpx;
					}
				}

				.icon-text {
					font-size: 26rpx;
					font-weight: 600;
					color: #333;
					text-align: center;
					line-height: 1.2;
				}
			}
		}
	}

	// 最新订单区域
	.orders-section {
		background: white;
		border-radius: 24rpx;
		padding: 32rpx;
		box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
		margin-top: 24rpx;

		.orders-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 24rpx;

			.section-title {
				display: flex;
				align-items: center;
				gap: 16rpx;

				.title-text {
					font-size: 32rpx;
					font-weight: 700;
					color: #333;
				}

				.title-decoration {
					width: 6rpx;
					height: 32rpx;
					background: linear-gradient(135deg, #FE6100, #ff8c42);
					border-radius: 3rpx;
				}
			}

			.more-link {
				display: flex;
				align-items: center;
				gap: 8rpx;
				padding: 12rpx 20rpx;
				background: rgba(254, 97, 0, 0.1);
				border-radius: 20rpx;
				transition: all 0.3s ease;

				&:active {
					transform: scale(0.95);
					background: rgba(254, 97, 0, 0.2);
				}

				.more-text {
					font-size: 24rpx;
					color: #FE6100;
					font-weight: 600;
				}
			}
		}

		.orders-content {
			// 订单列表内容样式由OrderCard组件内部处理
			min-height: 200rpx;
		}
	}


	// 兼容旧样式
	.header_slot {
		padding: 0 32rpx;
		width: 100%;
		font-weight: 500;
		font-size: 36rpx;
		color: #2C2622;
	}

	::v-deep .uni-noticebar {
		margin-bottom: 0 !important;
	}

	.index_top_image {
		width: 176rpx;
		height: 48rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.home_floor {
		padding: 24rpx;
		border-radius: 20rpx;
		background-color: #fff;

		.home_floor_title {
			font-size: 18px;
			font-weight: 600;
		}

		.home_floor_content {
			display: flex;
		}

		.home_floor_left {
			flex: 1;
			margin-right: 20rpx;
			position: relative;
			padding: 32rpx;
			background-color: #e9feeb;
			border-radius: 20rpx;

			.xq_img {
				position: absolute;
				right: 20rpx;
				bottom: 20rpx;
				width: 30%;
				height: 50%;
				z-index: 10;
			}

			.left_title {
				color: #0bcf56;
				font-size: 24px;
				font-weight: 700;
			}

			.left_text {
				padding: 8rpx 0;
				color: rgba(0, 0, 0, .6);
			}
		}

		.home_floor_right {
			display: flex;
			flex-direction: column;
			flex: 1;

			.floor_right_item {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: start;
				padding: 24rpx 32rpx;
				border-radius: 20rpx;
				background-color: #e6f7fe;
				margin-bottom: 20rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.item_title {
					display: flex;
					justify-content: space-between;
					align-items: end;
					width: 100%;
					font-size: 16px;
					font-weight: 700;

					image {
						width: 25px;
						height: 25px;
					}
				}

				.item_tips {
					color: rgba(0, 0, 0, .6);
				}
			}
		}
	}

	.home_floor_bottom {
		padding-top: 24rpx;
		display: flex;
		justify-content: space-between;
		align-items: flex-start;

		.bottom_item {
			flex: 0 0 22%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			align-items: center;

			image {
				height: 44rpx;
				width: 44rpx;
			}

			.bottom_item_text {
				padding-top: 20rpx;
				font-weight: 700;
				font-size: 26rpx;
				text-align: center;
				line-height: 1.2;
			}
		}
	}

	.cu-item-cur {
		font-size: 16px;
		font-weight: 700;
	}

	.nav {
		margin: 24rpx 0;
		background-color: #fff;

		.cu-item {
			padding: 0;
		}
	}
</style>